package com.wentao.mall.service.impl;

import com.wentao.mall.commons.OrderStatusEnum;
import com.wentao.mall.exception.MallException;
import com.wentao.mall.exception.MallExceptionEnum;
import com.wentao.mall.filter.CustomerFilter;
import com.wentao.mall.mapper.OrderItemMapper;
import com.wentao.mall.mapper.OrderMapper;
import com.wentao.mall.model.po.Order;
import com.wentao.mall.model.po.OrderItem;
import com.wentao.mall.model.po.OrderItemExample;
import com.wentao.mall.model.vo.OrderItemVo;
import com.wentao.mall.model.vo.OrderVo;
import com.wentao.mall.service.BackService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BackServiceImpl implements BackService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public List<OrderVo> orderListAll() {
        //找出所有订单
        List<Order> orders = orderMapper.selectByExample(null);
        //将order转为orderVo
        List<OrderVo> orderVoList = new ArrayList<>();
        for (Order order : orders) {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);
            orderVo.setOrderStatusName(OrderStatusEnum.codeOf(order.getOrderStatus()).getStatusName());
            //设置每个orderVo的orderItemVo项
            OrderItemExample orderItemExample = new OrderItemExample();
            orderItemExample.createCriteria().andOrderNoEqualTo(order.getOrderNo());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
            //将每个orderItem转为orderItemVo
            List<OrderItemVo> orderItemVoList = new ArrayList<>();
            for (OrderItem orderItem : orderItems) {
                OrderItemVo orderItemVo = new OrderItemVo();
                BeanUtils.copyProperties(orderItem, orderItemVo);
                orderItemVoList.add(orderItemVo);
            }
            orderItems = null;
            orderVo.setOrderItemList(orderItemVoList);
            orderVoList.add(orderVo);
        }
        return orderVoList;
    }

    @Override
    public void orderDelivered(String orderNo) {
        Order order = orderMapper.selectByOrderNo(orderNo);
        if (!order.getOrderStatus().equals(OrderStatusEnum.PAID.getStatusCode())) {
            throw new MallException(MallExceptionEnum.WRONG_ORDER_STATUS);
        }
        if (order.getOrderStatus() != OrderStatusEnum.PAID.getStatusCode()) {
            throw new MallException(MallExceptionEnum.DELIVERED_FAILED);
        }
        order.setOrderStatus(OrderStatusEnum.DELIVERED.getStatusCode());
        order.setUpdateTime(new Date());
        int count = orderMapper.updateByPrimaryKeySelective(order);
        if (count == 0) {
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public void orderFinish(String orderNo) {
        Order order = orderMapper.selectByOrderNo(orderNo);
        if (order == null) {
            throw new MallException(MallExceptionEnum.ORDER_NOT_FOUND);
        }
        //如果不是已发货状态就不能完成订单
        if (order.getOrderStatus() != OrderStatusEnum.DELIVERED.getStatusCode()) {
            throw new MallException(MallExceptionEnum.WRONG_ORDER_STATUS);
        }
        //如果是普通用户登录，判断orderNo是否为本用户
        if (CustomerFilter.currentUser.getRole()==1 && !CustomerFilter.currentUser.getId().equals(order.getUserId())) {
            throw new MallException(MallExceptionEnum.NOT_YOUR_ORDER);
        }
        order.setOrderStatus(OrderStatusEnum.FINISHED.getStatusCode());
        order.setUpdateTime(new Date());
        int count = orderMapper.updateByPrimaryKeySelective(order);
        if (count == 0) {
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }
}
